package net.babeuf.form.factory 
{
	import net.babeuf.form.conditions.*;

	/**
	 * ...
	 * @author Damien Pegoraro
	 */
	public class MetaConditionFactory 
	{
		
		static private function getConditionFromType( type : String, o : Object ): AbstractCondition 
		{
			var out : AbstractCondition ; 	
			
			switch( type ) 
			{
				case "MatchCondition" : 
					out = new MatchCondition( o.form.getField(String(o.field1)), o.form.getField(String(o.field2))  ) ; 						
				break ;			
				case "MatchCaptchaCondition" : 
					out = new MatchCaptchaCondition( o.form.getField(String(o.field)), o.captcha  ) ; 						
				break ;		
				case "AtLeastOneFilledCondition" : 
					out = new AtLeastOneFilledCondition() ; 						
				break ;		
				case "MatchValueCondition" : 
					out = new MatchValueCondition( o.form.getField(String(o.field)), o.value) ; 						
				break ;	
				case "MissmatchCondition" : 
					out = new MissmatchCondition( o.form.getField(String(o.field1)), o.form.getField(String(o.field2))  ) ;	
				break ;	
				case "ValidAgeCondition" : 
					out = new ValidAgeCondition( o.form.getField(String(o.day)), o.form.getField(String(o.month)), o.form.getField(String(o.year)), uint(o.age)  ) ;	
				break ;
				case "ValidDateCondition" : 
					out = new ValidDateCondition( o.form.getField(String(o.day)), o.form.getField(String(o.month)), o.form.getField(String(o.year)) ) ;	
				break ;
				default : 
					throw new Error( "Unknown condition type" ) ; 
				break ;
			}
			return out  ;
		}
		
		
		static public function buildConditionFromObject( o : Object ) : AbstractCondition
		{
			var out : AbstractCondition = getConditionFromType( o.type, o ) ; 
			var type : String = o.type ; 						
			if (  out ) 
			{
				if (out is AtLeastOneFilledCondition )
				{
					var tmp : Array = o.fields.split(",") ; 
					for each ( var s:  String in tmp )  AtLeastOneFilledCondition(out).addField( o.form.getField(s)) ; 
				}			
			}			
			return out ;			
		}
		
	}

}